package inabit.inference;

import inabit.data.Variable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
/**Node of the inference Trace tree
 * @author Rafael Rocha*/
public class InferenceNode extends DefaultMutableTreeNode{

	private static final long serialVersionUID = 1L;
	
	public Object content;
	
	private boolean conclusionNode;
	private boolean inconsistencyNode;
	private boolean verified;
	private boolean traceBack;
	private List<Variable> referencedVariables;
	
	public InferenceNode(String description, boolean conclusionNode){
		super(description);
		this.conclusionNode = conclusionNode;
		this.content = description;
		verified = false;
		referencedVariables = new ArrayList<Variable>();
	}
	public boolean wasVerified() {
		if(getChildCount() == 0) {
			return verified;
		}
		for(int i = 0; i < getChildCount(); i++) {
			InferenceNode child = (InferenceNode)getChildAt(i);
			if(child.wasVerified()) {
				return true;
			}
		}
		return false;
	}
	public void setVerified(boolean closed) {
		this.verified = closed;
	}
	
	public boolean isConclusionNode() {
		return conclusionNode;
	}
	
	public Object getContent(){
		return content;
	}
	
	public void setReferencedVariables(Collection<Variable> var) {
		this.referencedVariables.clear();
		this.referencedVariables.addAll(var);
	}
	public boolean isReferencedVariable(Variable var) {
		if(var != null) {
			if(referencedVariables.contains(var)) {
				return true;
			}
			for(int i = 0; i < getChildCount(); i++) {
				InferenceNode child = (InferenceNode)getChildAt(i);
				if(child.isReferencedVariable(var)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean isReferencedVariable(String varID) {
		if(varID != null) {
			for(Variable var : referencedVariables) {
				if(var.getID().equals(varID)){
					return true;
				}
			}
			for(int i = 0; i < getChildCount(); i++) {
				InferenceNode child = (InferenceNode)getChildAt(i);
				if(child.isReferencedVariable(varID)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean isInconsistencyNode() {
		if(getChildCount() == 0) {
			return inconsistencyNode;
		}
		for(int i = 0; i < getChildCount(); i++) {
			InferenceNode child = (InferenceNode)getChildAt(i);
			if(!child.isInconsistencyNode()) {
				return false;
			}
		}
		return true;
	}
	
	public void setIconsistencyNode(boolean inconsistency){
		this.inconsistencyNode = inconsistency;
	}
		
	
	@Override
	public boolean equals(Object n) {
		if(n instanceof InferenceNode) {
			InferenceNode node = (InferenceNode)n;
			if(node.getContent() == null || content == null){return false;}
			return node.getContent().equals(content);
		}
		return false;
	}

}
